package com.sports.controller;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.protobuf.Message;
import com.sports.dto.ScoreADTO;
import com.sports.dto.ScoreIDTO;
import com.sports.entity.Athletes;
import com.sports.entity.Score;
import com.sports.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("score")
public class ScoreController {
    @Autowired
    private ScoreService scoreService;

    @GetMapping("athlesName")
    public Map<String, Object> showScoreByAthName(String athletesName) {
        HashMap<String, Object> map = new HashMap<>();
        List<ScoreADTO> scoreDTOS = scoreService.showItemNameById(athletesName);
        if (ObjUtil.isEmpty(scoreDTOS)) {
            map.put("code", 400);
            map.put("message", "fail");
        } else {
            map.put("code", 200);
            map.put("message", "success");
        }
        map.put("data", scoreDTOS);
        return map;
    }

    @GetMapping("itemName/{pageNum}/{pageSize}")
    public Map<String, Object> showScoreByItemName(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize,String itemName) {
        HashMap<String, Object> map = new HashMap<>();
        Page<ScoreIDTO> page1 = scoreService.showAthletesNameById(pageNum,pageSize,itemName);
        if (ObjUtil.isEmpty(page1)) {
            map.put("code", 400);
            map.put("message", "fail");
        } else {
            map.put("code", 200);
            map.put("message", "success");
            List<ScoreIDTO> list = page1.getRecords();
            long page1Size = page1.getSize();
            long current = page1.getCurrent();
            long total = page1.getTotal();
            map.put("list",list);
            map.put("pageSize",page1Size);
            map.put("pageNum",current);
            map.put("total",total);
        }
        return map;
    }

    @PutMapping("update")
    public Map updateScore(@RequestBody Score score) {
        Map map = new HashMap();
        try {
            scoreService.updateById(score);
            map.put("message", "success");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "error");
        }
        return map;
    }

    @GetMapping("all")
    public Score showScore(Integer scoreId) {
        Score score = scoreService.getById(scoreId);
        return score;
    }
}
